package com.liukai.bishe.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.transaction.Transaction;
import com.liukai.bishe.model.doman.transaction.TransactionUseTime;
import com.liukai.bishe.ui.dialog.TransactionDetailsDialog;
import com.liukai.bishe.ui.viewHolder.BaseViewHolder;
import com.liukai.bishe.ui.viewHolder.StateViewHolder;
import com.liukai.bishe.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class StateRecyclerViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private List<TransactionUseTime> mTransactionUseTimeList;
    private Context mContext;

    public StateRecyclerViewAdapter(List<TransactionUseTime> transactionUseTimeList, Context context) {


        this.mTransactionUseTimeList = transactionUseTimeList;
        this.mContext = context;

//        long time = System.currentTimeMillis();
//        ToastUtil.log(mContext,"时间戳",time+"");
//
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        Date date = new Date(time);
//        String dateTime = simpleDateFormat.format(date);
//        ToastUtil.log(mContext,"当前时间",dateTime+"");

    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = View.inflate(parent.getContext(),R.layout.item_recyclerview_state,null);

        return new StateViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {

        StateViewHolder stateViewHolder = (StateViewHolder)holder;
        TransactionUseTime transactionUseTime = mTransactionUseTimeList.get(position);

        long startTime = Long.valueOf(transactionUseTime.getStarttime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日"+"\n"+"HH时mm分");
        Date date = new Date(startTime);
        String startTimeString = simpleDateFormat.format(date);

        stateViewHolder.tvTitle.setText(transactionUseTime.getTransactionname());
        stateViewHolder.tvStartTime.setText("开始于:"+startTimeString);

        stateViewHolder.btnDetails.setOnClickListener(view -> {

            TransactionDetailsDialog transactionDetailsDialog = new TransactionDetailsDialog(mContext,transactionUseTime);
            transactionDetailsDialog.show();

        });

    }

    @Override
    public int getItemCount() {
        return mTransactionUseTimeList.size();
    }
}
